﻿Imports Negocio
Imports Excepciones
Imports DataLayer

Public Class GesTareas

    Public Shared Sub Guardar(tarea As Tarea, proyecto As Proyecto)

        Try
            If tarea.Usuario Is Nothing Then
                tarea.Usuario = Current.Usuario
            End If
            If proyecto Is Nothing Then
                proyecto = New Proyecto()
            End If

            Dim repository As IRepository = RepositoryFactory.CreateTransactional()
            repository.Nombre = "TAREAS_PROC"
            repository.AddParameter("@ACCION", tarea.Persistencia)
            repository.AddParameter("@ID", tarea.OID)
            repository.AddParameter("@TITULO", tarea.Titulo)
            repository.AddParameter("@DESCRIPCION", tarea.Descripcion)
            repository.AddParameter("@FECHA", DateTime.Now)
            repository.AddParameter("@FECHAINICIO", tarea.FechaInicio)
            repository.AddParameter("@PRIORIDAD", tarea.Prioridad)
            repository.AddParameter("@HORAS", tarea.Horas)
            repository.AddParameter("@IDPROYECTO", proyecto.OID)
            repository.AddParameter("@IDUSUARIO", tarea.Usuario.OID)
            repository.ExecuteNonQuery()
            repository.Commit()
        Catch ex As Exception
            Throw New TareaSaveException()
        End Try

        GesBitacoras.Guardar(tarea.Persistencia, "ModTarea", New String() {"Tarea", tarea.Titulo})

    End Sub

    Public Shared Sub Eliminar(tarea As Tarea)
        Eliminar(tarea, True)
    End Sub

    Public Shared Sub Eliminar(tarea As Tarea, control As Boolean)

        If control AndAlso GesActividades.Consultar(tarea).Count > 0 Then
            Throw New TareaActivityDeleteException()
        Else
            tarea.Persistencia = EstadoPersistencia.DELETE
            Guardar(tarea, Nothing)
        End If

    End Sub

    Public Shared Function CalcularHoras(proyecto As Proyecto) As Integer
        Try
            Dim horas As Integer = 0
            Dim tareas As List(Of Tarea) = Consultar(proyecto)
            For Each tarea As Tarea In tareas
                horas = horas + CalcularHoras(tarea)
            Next
            Return horas
        Catch ex As Exception
            Throw New TareaGetException()
        End Try
    End Function

    Public Shared Function CalcularHoras(tarea As Tarea) As Integer
        Return tarea.Horas
    End Function


    Public Shared Function CalcularProgreso(tarea As Tarea) As Decimal

        Try
            Dim progress As Decimal = 0
            For Each act As Actividad In GesActividades.Consultar(tarea)
                progress += act.Progreso
            Next
            Return Math.Round(progress, 1)
        Catch ex As Exception
            Throw New TareaGetException()
        End Try

    End Function

    Public Shared Function CalcularProgreso(tarea As Tarea, persona As Recurso) As Decimal
        Dim h As Decimal = 0
        Return h
    End Function


    Public Shared Function TareasPendientes(proyecto As Proyecto) As List(Of Tarea)

        Try
            Dim tareas As List(Of Tarea) = Consultar(proyecto)
            Dim Pendientes As New List(Of Tarea)

            For Each tar As Tarea In tareas
                If CalcularProgreso(tar) < 100 Then
                    Pendientes.Add(tar)
                End If
            Next
            Return Pendientes
        Catch ex As Exception
            Throw New TareaGetException()
        End Try

    End Function


    Public Shared Function Consultar(depende As Tarea) As List(Of Tarea)
        'Cada Objeto EstadoPersistencia.UPDATE
        Dim t As New List(Of Tarea)
        Return t
    End Function

    Public Shared Function Consultar(proyecto As Proyecto) As List(Of Tarea)
        Return Consultar(proyecto, Nothing, Nothing, Nothing, Nothing)
    End Function
    Public Shared Function Consultar(recurso As Usuario) As List(Of Tarea)
        Return Consultar(Nothing, Nothing, Nothing, recurso, Nothing)
    End Function
    Public Shared Function Consultar(fecha As System.Nullable(Of Date), proyecto As Proyecto, recurso As Usuario) As List(Of Tarea)
        Return Consultar(proyecto, Nothing, fecha, recurso, Nothing)
    End Function

    Public Shared Function Consultar(proyecto As Proyecto, titulo As String, fecha As System.Nullable(Of Date), usuario As Usuario, prioridad As Nivel) As List(Of Tarea)

        If String.IsNullOrWhiteSpace(titulo) Then
            titulo = Nothing
        End If

        If usuario Is Nothing Then
            usuario = New Usuario()
            usuario.OID = Nothing
        End If

        Dim tareas As New List(Of Tarea)
        Try
            Dim repository As IRepository = RepositoryFactory.Create()
            repository.Nombre = "TAREAS_PROC"
            repository.AddParameter("@ACCION", "4")
            repository.AddParameter("@ID", Nothing)
            repository.AddParameter("@TITULO", titulo)
            repository.AddParameter("@DESCRIPCION", Nothing)
            repository.AddParameter("@FECHA", Nothing)
            repository.AddParameter("@FECHAINICIO", fecha)
            repository.AddParameter("@PRIORIDAD", prioridad)
            repository.AddParameter("@HORAS", Nothing)
            repository.AddParameter("@IDPROYECTO", proyecto.OID)
            repository.AddParameter("@IDUSUARIO", usuario.OID)
            Dim dtTarea As DataTable = repository.ExecuteDataTable()

            For Each dr As DataRow In dtTarea.Rows
                Dim t As New Tarea()
                t.Persistencia = EstadoPersistencia.UPDATE
                t.OID = Convert.ToInt32(dr.Item("tarID"))
                t.Titulo = dr.Item("tarTITULO").ToString()
                t.Descripcion = dr.Item("tarDESCRIPCION").ToString()
                t.Fecha = Convert.ToDateTime(dr.Item("tarFECHA"))
                t.FechaInicio = Convert.ToDateTime(dr.Item("tarFECHAINICIO"))
                t.Horas = Convert.ToInt32(dr.Item("tarHORAS"))
                t.Prioridad = Convert.ToInt32(dr.Item("tarPRIORDAD"))
                t.Usuario = GesUsuarios.Obtener(dr.Item("usuNICK").ToString(), True)

                tareas.Add(t)
            Next

        Catch ex As Exception
            Throw New TareaGetException()
        End Try

        Return tareas
    End Function

    Public Shared Function ObtenerRecursos(tarea As Tarea) As List(Of Recurso)

        Dim recursos As New List(Of Recurso)
        Try
            Dim repository As IRepository = RepositoryFactory.Create()
            repository.Nombre = "TAREA_RECURSO_PROC"
            repository.AddParameter("@ACCION", "4")
            repository.AddParameter("@IDTAREA", tarea.OID)
            repository.AddParameter("@IDRECURSO", Nothing)
            Dim dtRecurso As DataTable = repository.ExecuteDataTable()

            For Each dr As DataRow In dtRecurso.Rows
                Dim recID As Integer = Convert.ToInt32(dr.Item("trIDRECURSO"))
                recursos.Add(GesRecursos.Obtener(recID))
            Next

        Catch ex As Exception
            Throw New ResourceGetException()
        End Try

        Return recursos

    End Function

    Public Shared Sub ActualizarRecursos(tarea As Tarea, recursos As List(Of Recurso))
        For Each rec As Recurso In recursos
            If Not rec.Persistencia = EstadoPersistencia.UPDATE Then
                GuardarRecurso(tarea, rec)
            End If
        Next
    End Sub

    Public Shared Sub GuardarRecurso(tarea As Tarea, recurso As Recurso)
        Try
            Dim repository As IRepository = RepositoryFactory.Create()
            repository.Nombre = "TAREA_RECURSO_PROC"
            repository.AddParameter("@ACCION", recurso.Persistencia)
            repository.AddParameter("@IDTAREA", tarea.OID)
            repository.AddParameter("@IDRECURSO", recurso.OID)
            repository.ExecuteNonQuery()
            repository.Commit()
        Catch ex As Exception
            Throw New ResourceSaveException()
        End Try

        GesBitacoras.Guardar(recurso.Persistencia, "ModTarea", New String() {"Recurso", recurso.ToString()})
    End Sub

    Public Shared Sub AgregarActividad(tarea As Tarea, actividad As Actividad)

    End Sub

    Public Shared Sub QuitarActividad(tarea As Tarea, actividad As Actividad)

    End Sub


    Public Shared Sub AgregarArchivo(tarea As Tarea, archivo As Archivo)

    End Sub

    Public Shared Sub QuitarArchivo(tarea As Tarea, archivo As Archivo)

    End Sub


End Class
